const lists = require('../../models/list')
const goods = require('../../models/goods')

const address = require('../../models/address')

const sequelize = require("../../models/db.js")
const { QueryTypes } = require('sequelize');


exports.getUserlistAll = async function (userId) {
    // console.log(userId);
    const goodsid = await lists.findAll({ //查询配置，查询条件
        where: {
            userId,
        },
        attributes: ['goodsId'],
        raw: true
    }).catch(()=>{})
   let a = goodsid.map(item=>item.goodsId);
    const result = await goods.findAll({ //查询配置，查询条件
        where: {
            id:a
        },
        raw: true
    }).catch(()=>{})
    if (result != '') return {
        data: result,
        meta: {
            msg: '查询成功',
            status: '200'
        }
    }
    return {
        data: {},
        meta: {
            msg: '暂无数据或用户不存在',
            status: '404'
        }
    }
}

// <<<<<<< HEAD
// exports.getUserlist = async function (userId,status) {
//     // console.log(userId);
//     const goodsid = await lists.findAll({ //查询配置，查询条件
//         where: {
//             userId,status
//         },
//         attributes: ['goodsId'],
// =======
exports.getUserlist = async function () {

    // if(!pageNum ||! pageSize) {
    //     const result1 = await sequelize.query("SELECT * FROM `lists`")
    //     type: QueryTypes.SELECT
    // }
    // const result1 = await lists.findAndCountAll( {
    //     type: QueryTypes.SELECT,
    //     limit:parseInt(pageSize),
    //     offset:parseInt((pageNum-1) * pageSize),
    //     raw:true 
    // })
    // console.log(userId);
    const goodsid = await lists.findAll({ //查询配置，查询条件
        
        // attributes: ['goodsId','id'],
// >>>>>>> origin/xwz
        raw: true
    }).catch(()=>{})
   let a = goodsid.map(item=>item.goodsId);
    const result = await goods.findAll({ //查询配置，查询条件
        where: {
            id:a
        },
        raw: true
    }).catch(()=>{})
    console.log(result);
    console.log(goodsid);
    for(var i = 0;i<goodsid.length;i++){
        for(var j = 0;j<result.length;j++){
            if(goodsid[i].goodsId == result[j].id){
                goodsid[i].price = result[j].price
        }
    }
        
    }
    console.log(goodsid);
    if (result != '') return {
        data: goodsid,
        meta: {
            msg: '查询成功',
            status: '200'
        }
    }
    return {
        data: {},
        meta: {
            msg: '暂无数据或用户不存在',
            status: '404'
        }
    }
}


exports.addUserList = async function (userId,goodsId,status) {
    const result = await lists.findAll({ //查询配置，查询条件
        where: {
            userId,goodsId
        },
        raw: true
    }).catch(()=>{})
    console.log(result);
    for(var i = 0;i<goodsId.length;i++){
        for(var j = 0;j<result.length;j++){
            if(goodsId[i] == result[j].goodsId){
                goodsId.splice(i,1)
            }
        }
    }
    if(result != '') return
    // if(result != '' && result.length == goodsId.length)
    // console.log(result);
    for(var i = 0 ;i<goodsId.length;i++){
        lists.create({
            userId,
            goodsId:goodsId[i],
            status
        }).catch(()=>{})
    }
    return true
}
exports.getaddress = async function (userId) {
    const result = await address.findAll({ //查询配置，查询条件
        where: {
            userId,
        },
        raw: true
    }).catch(() => { })
    console.log(result);
    return result
}
